#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Text;


namespace minesweeper {

	/// <summary>
	/// Summary for signup
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class signup : public System::Windows::Forms::Form
	{
	public:
		signup(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~signup()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label5;
	protected: 
	private: System::Windows::Forms::Button^  btn_signup;


	private: System::Windows::Forms::TextBox^  txt_password;
	private: System::Windows::Forms::TextBox^  txt_username;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  txt_retype;

	private: System::Windows::Forms::Label^  label6;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->btn_signup = (gcnew System::Windows::Forms::Button());
			this->txt_password = (gcnew System::Windows::Forms::TextBox());
			this->txt_username = (gcnew System::Windows::Forms::TextBox());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->txt_retype = (gcnew System::Windows::Forms::TextBox());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Font = (gcnew System::Drawing::Font(L"PMingLiU", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label5->Location = System::Drawing::Point(243, 240);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(90, 10);
			this->label5->TabIndex = 17;
			this->label5->Text = L"Copyright by Team 1";
			// 
			// btn_signup
			// 
			this->btn_signup->Location = System::Drawing::Point(229, 184);
			this->btn_signup->Name = L"btn_signup";
			this->btn_signup->Size = System::Drawing::Size(75, 23);
			this->btn_signup->TabIndex = 16;
			this->btn_signup->Text = L"Sign Up";
			this->btn_signup->UseVisualStyleBackColor = true;
			this->btn_signup->Click += gcnew System::EventHandler(this, &signup::btn_signup_Click);
			// 
			// txt_password
			// 
			this->txt_password->Location = System::Drawing::Point(99, 114);
			this->txt_password->Name = L"txt_password";
			this->txt_password->Size = System::Drawing::Size(169, 22);
			this->txt_password->TabIndex = 14;
			this->txt_password->UseSystemPasswordChar = true;
			// 
			// txt_username
			// 
			this->txt_username->Location = System::Drawing::Point(99, 79);
			this->txt_username->Name = L"txt_username";
			this->txt_username->Size = System::Drawing::Size(169, 22);
			this->txt_username->TabIndex = 13;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label4->Location = System::Drawing::Point(24, 117);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(57, 14);
			this->label4->TabIndex = 12;
			this->label4->Text = L"Password";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label3->Location = System::Drawing::Point(24, 82);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(66, 14);
			this->label3->TabIndex = 11;
			this->label3->Text = L"User Name";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label2->Location = System::Drawing::Point(140, 50);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(52, 14);
			this->label2->TabIndex = 10;
			this->label2->Text = L"Sign Up";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"PMingLiU", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label1->Location = System::Drawing::Point(96, 34);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(172, 16);
			this->label1->TabIndex = 9;
			this->label1->Text = L"Welcome to MineSweeper";
			// 
			// txt_retype
			// 
			this->txt_retype->Location = System::Drawing::Point(99, 147);
			this->txt_retype->Name = L"txt_retype";
			this->txt_retype->Size = System::Drawing::Size(169, 22);
			this->txt_retype->TabIndex = 19;
			this->txt_retype->UseSystemPasswordChar = true;
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label6->Location = System::Drawing::Point(24, 150);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(54, 14);
			this->label6->TabIndex = 18;
			this->label6->Text = L"Re-Type";
			// 
			// signup
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(338, 255);
			this->Controls->Add(this->txt_retype);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->btn_signup);
			this->Controls->Add(this->txt_password);
			this->Controls->Add(this->txt_username);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->MaximizeBox = false;
			this->MaximumSize = System::Drawing::Size(346, 289);
			this->MinimumSize = System::Drawing::Size(346, 289);
			this->Name = L"signup";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"signup";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void btn_signup_Click(System::Object^  sender, System::EventArgs^  e) {
				 if (check_signup()){
					 if (check_exist()){
						 //add user into UserList.txt
						   String^ path = "UserList.txt";
						   String^ path2 = this->txt_username->Text + ".txt";
						   FileStream^ fs = gcnew FileStream( path,FileMode::Append,FileAccess::Write,FileShare::Write );
						   fs->Close();
						   StreamWriter^ sw = gcnew StreamWriter( path,true,Encoding::ASCII );
						   String^ NextLine = this->txt_username->Text+"\n";
						   sw->Write( NextLine );
						   sw->Close();
						   //create User profile file
						   fs = gcnew FileStream( path2,FileMode::Append,FileAccess::Write,FileShare::Write );
						   fs->Close();
						   sw = gcnew StreamWriter( path2,true,Encoding::ASCII );
						   NextLine = this->txt_password->Text+"\n";
						   sw->Write( NextLine );
						   sw->Close();
						   // welcome msg and close the window form
						   String^ message = "User Created!";
						   String^ title = "Welcome to MineSweeper!";
						   MessageBox::Show(message,title,MessageBoxButtons::OK);
						  Close();
			 }
				 }
			 }

			 // check user wheather existed in our database
	 public: bool check_exist(){
		try
        {
            StreamReader^ sr = gcnew StreamReader("UserList.txt");
            String^ line;
            while ((line = sr->ReadLine()) != nullptr)
            {
				if (line == this->txt_username->Text ){
				String^ message = "Username Existed!";
				String^ title = "Error Message";
				MessageBox::Show(message,title,MessageBoxButtons::OK);
				return false;
				}
            }
            sr->Close();
        }
        catch (Exception^ e)
        {
            Console::WriteLine("The file could not be read:check_exist");
            Console::WriteLine(e->Message);
        }
		return true;
		
	}

		//check the input validation
	public: bool check_signup(){
				String^ message = "";
				String^ title = "Error Message";

				if (this->txt_username->Text == "") {
					message += "Please enter a username!\n";
				}
				if (this->txt_password->Text == "") {
					message += "Please enter a password!\n";
				}
				if (this->txt_password->Text != "" && this->txt_retype->Text == "") {
					message += "Please retype a password!\n";
				}
				if (this->txt_password->Text != this->txt_retype->Text ) {
					message += "Please make sure the password is correct!\n";
				}
				if (message != ""){
				MessageBox::Show(message,title,MessageBoxButtons::OK);
				return false;
				}
				else 
				return true;
	}
};
}
